package com.fangngng.pushall.dto;


import com.fangngng.pushall.constant.ErrorCode;

/**
 * @author:fangn
 * @Date:2020/2/13
 **/
public class Result<T> {

    public static String CODE_SUCCESS = "0";

    public static String CODE_FAIL = "9999";

    private String code;

    private String message;

    private T data;

    public Result(String code) {
        this.code = code;
    }

    public Result(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.message = errorCode.getMessage();
    }

    public Result(ErrorCode errorCode, T data) {
        this.code = errorCode.getCode();
        this.message = errorCode.getMessage();
        this.data = data;
    }

    public static <P> Result<P> ok(){
        return new Result<P>(CODE_SUCCESS, "success", null);
    }

    public static <P> Result<P> ok(P data){
        return new Result<P>(CODE_SUCCESS, "success", data);
    }

    public static <P> Result<P> fail(){
        return new Result<P>(CODE_FAIL, "fail", null);
    }

    public static <P> Result<P> fail(P data){
        return new Result<P>(CODE_FAIL, "fail", data);
    }

    public static <P> Result<P> fail(String msg){
        return new Result<P>(CODE_FAIL, msg, null);
    }

    public Boolean isSuccess(){
        return CODE_SUCCESS.equals(this.getCode());
    }

    public Boolean isFail(){
        return CODE_FAIL.equals(this.getCode());
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
